import { IComponent } from "./base/IComponent";
import { Ref } from "../decorators";

export class NavRouter {
    @Ref
    private routeStack: Array<string> = [];
    @Ref
    private paramStack: Map<string, any> = new Map;

    push(name: string, param: any): void {
        this.routeStack.push(name);
        this.paramStack.set(name, param);
    }

    getStack() {
        return this.routeStack;
    }

    getParam<T>(name: string): T {
        return this.paramStack.get(name);
    }

    getTop() {
        return this.routeStack[this.routeStack.length - 1];
    }
}

class _Router extends IComponent {
    public static id: number = 0;
    private _element: HTMLDivElement = document.createElement('div');
    private _route: Record<string, IComponent> = {};
    private _router: NavRouter = new NavRouter();

    constructor(router: NavRouter) {
        super(`navigation-${_Router.id++}`);
        this._router = router;
    }

    route(route: Record<string, IComponent>): this {
        this._route = route;
        return this;
    }

    build(_parentId?: string) {
        if (this.getTopComponent() == null) {
            this._children = this._children.slice(0, 1);
        } else {

        }
        super._build(this._element, _parentId);
    }

    private getTopComponent() {
        const top = this._router.getTop();
        if (top) {
            return this._route[top];
        }
        return null;
    }
}

export function Router(router: NavRouter) {
    return new _Router(router);
}